package com.yorun.lib_wx_pay;

import android.util.Log;
import android.util.Pair;
import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by yorun on 2016/12/21.
 */

public class Utils {

    public static String objectToXml(Object object ,String root) {
        ByteArrayOutputStream baos = null;
        try {

            XmlSerializer serializer = Xml.newSerializer();

            baos = new ByteArrayOutputStream();

            serializer.setOutput(baos , "utf-8");

            serializer.startDocument("utf-8", true);

            serializer.startTag(null, root);

            List<Method> getMethods = getGetMethods(object.getClass());

            for (Method method : getMethods) {
                String fieldNameByMethodName = getFieldNameByMethodName(method.getName());
                serializer.startTag(null, fieldNameByMethodName);
                Object value = method.invoke(object);
                serializer.text(value==null?"null":value.toString());
                serializer.endTag(null, fieldNameByMethodName);
            }
            serializer.endTag(null, root);
            serializer.endDocument();
            baos.close();
            return baos.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (baos!=null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }

        }
        return null;
    }

    public static String getFieldNameByMethodName(String getOrSetName) {

        String fieldName = getOrSetName.substring(3);

        fieldName = fieldName.substring(0, 1).toLowerCase()
                + fieldName.substring(1);

        return fieldName;
    }
    public static List<Method> getGetMethods(Class beanClass) {

        List<Method> methods = new ArrayList<Method>();

        Method[] declaredMethods = beanClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            String methodName = method.getName();
            if (methodName.startsWith("get") && (!method.equals("getClass"))) {
                methods.add(method);
            }
        }
        return methods;
    }
    public static String genPackageSign(Pair[] params) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < params.length; i++) {
            sb.append(params[i].first);
            sb.append('=');
            sb.append(params[i].second);
            sb.append('&');
        }
        sb.append("key=");
        sb.append(YWXPays.config.mchKey);
        String packageSign = MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();
        Log.e("orion",packageSign);
        return packageSign;
    }
    public static  String genNonceStr() {
        Random random = new Random();
        return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }
}
